package mvp.cklm.testtask.converters;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import mvp.cklm.testtask.model.AddressModel;
import mvp.cklm.testtask.model.PhoneNumber;
import mvp.cklm.testtask.model.enums.PhoneType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: WORKSPOT2
 * Date: 24.02.14
 * Time: 21:10
 * To change this template use File | Settings | File Templates.
 */
public class AddressModelConverter implements IMongoDBConverter<AddressModel> {

    @Override
    public DBObject convertToDBObject(AddressModel addressModel) {
        DBObject toSend = new BasicDBObject("_id", addressModel.getId());
        toSend.put("country", addressModel.getCountry());
        toSend.put("city", addressModel.getCity());
        toSend.put("street", addressModel.getStreet());
        toSend.put("houseNumber", addressModel.getHouseNumber());
        toSend.put("flatNumber", addressModel.getFlatNumber());
        toSend.put("comment", addressModel.getCommment());
        toSend.put("numbers", convertNumbersToDBObject(addressModel.getNumbers()));
        return toSend;
    }

    @Override
    public AddressModel convertFromDBObject(DBObject dbObject) {
        Integer id = (Integer) dbObject.get("_id");
        String country = (String) dbObject.get("country");
        String city = (String) dbObject.get("city");
        String street = (String) dbObject.get("street");
        String comment = (String) dbObject.get("comment");
        Integer houseNumber = (Integer) dbObject.get("houseNumber");
        Integer flatNumber = (Integer) dbObject.get("flatNumber");
        List<PhoneNumber> numbers = convertDBObjectToNumbers((Collection<DBObject>) dbObject.get("numbers"));
        return new AddressModel(id, country, city, street, houseNumber, flatNumber, comment, numbers);
    }

    private List<DBObject> convertNumbersToDBObject(Collection<PhoneNumber> numbers) {
        List<DBObject> toSend = new LinkedList<DBObject>();
        Iterator<PhoneNumber> iter = numbers.iterator();
        PhoneNumber tmp;
        DBObject tmpObject;
        while (iter.hasNext()) {
            tmp = iter.next();
            tmpObject = new BasicDBObject("type", tmp.getType().name());
            tmpObject.put("phoneNumber", tmp.getPhoneNumber());
            toSend.add(tmpObject);
        }
        return toSend;
    }

    private List<PhoneNumber> convertDBObjectToNumbers(Collection<DBObject> objects) {
        List<PhoneNumber> toSend = new LinkedList<PhoneNumber>();
        Iterator<DBObject> iter = objects.iterator();
        DBObject tmp;
        PhoneNumber tmpNumber;
        while (iter.hasNext()) {
            tmp = iter.next();
            tmpNumber = new PhoneNumber(PhoneType.valueOf((String)tmp.get("type")), (String) tmp.get("phoneNumber"));
            toSend.add(tmpNumber);
        }
        return toSend;
    }
}
